package com.htfyun.factorytest.factorytestui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.htfyun.factorytest.FTFloatWindowService;
import com.htfyun.factorytest.FullScreenBaseActivity;
import com.htfyun.factorytest.ViewDialogFragment;

import java.lang.ref.WeakReference;

import com.htfyun.factorytest.R;

import com.htfyun.factorytest.utils.DebugLog;

public abstract class FactoryBaseActivity extends FullScreenBaseActivity {

    private Button btnPass;
    private Button btnFail;

    protected Activity mContext;
    private MyHandler<? extends FactoryBaseActivity> myHandler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myHandler = new MyHandler<>(this);
        DebugLog.e("onCreate this = " + this+ ",handler=" + myHandler );
        setContentView(getLayoutId());
        mContext = this;

        btnPass = findViewById(R.id.btnPass);
        btnFail = findViewById(R.id.btnFail);

        if (btnPass != null) {
            btnPass.setOnClickListener(onClickListener);
        }

        if (btnFail != null) {
            btnFail.setOnClickListener(onClickListener);
        }

        onInitView(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        DebugLog.e("onResume this = " + this);
        FTFloatWindowService.setFloatLayoutShow(false);
    }

    @Override
    protected void onPause() {
        super.onPause();
        DebugLog.e("onPause this = " + this);
        FTFloatWindowService.setFloatLayoutShow(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DebugLog.e("onDestroy this = " + this);
    }

    protected void setBtnPassVisibility(int visibility) {
        if (btnPass != null) {
            btnPass.setVisibility(visibility);
        }
    }

    protected void setPassButtonEnable(boolean enabled) {
        if (btnPass != null) {
            btnPass.setEnabled(enabled);
        }
    }

    protected void finishWithOK() {
        setResult(RESULT_OK);
        ActivityCompat.finishAfterTransition(this);
    }

    protected void finishWithFail() {
        setResult(RESULT_CANCELED);
        ActivityCompat.finishAfterTransition(this);
    }

    public Button getBtnFail() {
        return btnFail;
    }

    public Button getBtnPass() {
        return btnPass;
    }

    protected void showFinishDialog(int titleRes, int msgRes) {
        final String title = getString(titleRes);
        final String msg = getString(msgRes);
        ViewDialogFragment dialog = ViewDialogFragment.newInstance(title, msg, false);

        dialog.show(getFragmentManager(),
                this::finishWithOK,
                this::finishWithFail);
    }


    private View.OnClickListener onClickListener =
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (v == null) {
                        return;
                    }

                    if (btnPass != null && v == btnPass) {
                        finishWithOK();
                    }
                    if (btnFail != null && v == btnFail) {
                        finishWithFail();
                    }

                }
            };

    @NonNull
    protected Handler getHandler() {

        return myHandler;
    }

    @NonNull
    protected Context getContext() {
        return mContext;
    }

    private static final class MyHandler<T extends FactoryBaseActivity> extends Handler {


        private final WeakReference<T> reference;

        public MyHandler(T activity) {
            reference = new WeakReference<T>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            T activity = reference.get();
            if (activity == null) {
                return;
            }

            activity.onHandleMessage(this, msg);
        }
    }

    abstract int getLayoutId();

    abstract void onInitView(@Nullable Bundle savedInstanceState);

    abstract void onHandleMessage(Handler handler, Message msg);

}
